package cn.minhx.config;

import java.util.concurrent.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;

@Slf4j
@EnableAsync
@Configuration
@EnableConfigurationProperties(ThreadPoolConfigProperties.class)
public class ThreadPoolConfig {

	@Bean
	@ConditionalOnMissingBean(ThreadPoolExecutor.class)
	public ThreadPoolExecutor threadPoolExecutor(ThreadPoolConfigProperties properties)
			throws ClassNotFoundException, InstantiationException, IllegalAccessException {
		// 实例化策略
		RejectedExecutionHandler handler;
		switch (properties.getPolicy()) {
			case "AbortPolicy" :
				handler = new ThreadPoolExecutor.AbortPolicy();
				break;
			case "DiscardPolicy" :
				handler = new ThreadPoolExecutor.DiscardPolicy();
				break;
			case "DiscardOldestPolicy" :
				handler = new ThreadPoolExecutor.DiscardOldestPolicy();
				break;
			case "CallerRunsPolicy" :
				handler = new ThreadPoolExecutor.CallerRunsPolicy();
				break;
			default :
				handler = new ThreadPoolExecutor.AbortPolicy();
				break;
		}
		// 创建线程池
		return new ThreadPoolExecutor(properties.getCorePoolSize(), properties.getMaxPoolSize(),
				properties.getKeepAliveTime(), TimeUnit.SECONDS,
				new LinkedBlockingQueue<>(properties.getBlockQueueSize()), Executors.defaultThreadFactory(), handler);
	}
}
